package com.shenqi.language.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.shenqi.language.domain.Category;
import com.shenqi.language.mapper.CategoryMapper;
import com.shenqi.language.util.UserMarkEnum;
import com.shenqi.language.vo.VocabularyWithUserMarkVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.shenqi.language.mapper.VocabularyMapper;
import com.shenqi.language.domain.Vocabulary;
import com.shenqi.language.service.IVocabularyService;

/**
 * 词条Service业务层处理
 * 
 * @author shenqi
 * @date 2025-08-05
 */
@Service
public class VocabularyServiceImpl implements IVocabularyService 
{
    @Autowired
    private VocabularyMapper vocabularyMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    /**
     * 查询词条
     * 
     * @param id 词条主键
     * @return 词条
     */
    @Override
    public Vocabulary selectVocabularyById(Long id)
    {
        return vocabularyMapper.selectVocabularyById(id);
    }

    /**
     * 查询词条列表
     * 
     * @param vocabulary 词条
     * @return 词条
     */
    @Override
    public List<Vocabulary> selectVocabularyList(Vocabulary vocabulary)
    {
        return vocabularyMapper.selectVocabularyList(vocabulary);
    }

    /**
     * 新增词条
     * 
     * @param vocabulary 词条
     * @return 结果
     */
    @Override
    public int insertVocabulary(Vocabulary vocabulary)
    {
        vocabulary.setCreateTime(DateUtils.getNowDate());
        return vocabularyMapper.insertVocabulary(vocabulary);
    }

    /**
     * 修改词条
     * 
     * @param vocabulary 词条
     * @return 结果
     */
    @Override
    public int updateVocabulary(Vocabulary vocabulary)
    {
        vocabulary.setUpdateTime(DateUtils.getNowDate());
        return vocabularyMapper.updateVocabulary(vocabulary);
    }

    /**
     * 批量删除词条
     * 
     * @param ids 需要删除的词条主键
     * @return 结果
     */
    @Override
    public int deleteVocabularyByIds(Long[] ids)
    {
        return vocabularyMapper.deleteVocabularyByIds(ids);
    }

    /**
     * 删除词条信息
     * 
     * @param id 词条主键
     * @return 结果
     */
    @Override
    public int deleteVocabularyById(Long id)
    {
        return vocabularyMapper.deleteVocabularyById(id);
    }

    /**
     * 查询某个分类下的单词，分类节点包括下面的所有子节点
     * @param categoryIds
     * @return
     */
    @Override
    public List<Vocabulary> findVocabularyUnderCategory(List<Long> categoryIds) {
        List<Vocabulary> vocabularyList = vocabularyMapper.selectVocabularyByCategoryIds(categoryIds);
        return vocabularyList;
    }

    @Override
    public List<VocabularyWithUserMarkVO> selectByCategoryIdsWithUserMark(List<Long> categoryIds, Integer userMark) {
        // 获取当前用户ID
        Long userId = SecurityUtils.getUserId();
        return vocabularyMapper.selectByCategoryIdsWithUserMark(categoryIds, userId, userMark);
    }

    @Override
    public List<Vocabulary> selectRandomVocabularyList(List<Long> categoryIds) {
        return vocabularyMapper.selectRandomVocabularyByCategoryIds(categoryIds);
    }

    @Override
    public List<VocabularyWithUserMarkVO> selectRandomVocabularyByCategoryIdsAndUserMark(List<Long> categoryIds, Integer userMark, Long userId, Boolean isSorted) {
        return vocabularyMapper.selectRandomVocabularyByCategoryIdsAndUserMark(categoryIds, userMark, userId, isSorted);
    }

    @Override
    public Map<String, Integer> countUserMarksByCategoryIds(List<Long> categoryIds, Long userId){
        Map<String, Integer> counts = new HashMap<>();
        Integer unmarkCount = vocabularyMapper.countByCategoryIdsAndUserMark(categoryIds, UserMarkEnum.ALL.getValue(), userId);
        counts.put(UserMarkEnum.ALL.name(), unmarkCount);
        Integer notMarkCount = vocabularyMapper.countByCategoryIdsAndUserMark(categoryIds, UserMarkEnum.NOT_MARK.getValue(), userId);
        counts.put(UserMarkEnum.NOT_MARK.name(), notMarkCount);
        Integer sureCount = vocabularyMapper.countByCategoryIdsAndUserMark(categoryIds, UserMarkEnum.LEARNED.getValue(), userId);
        counts.put(UserMarkEnum.LEARNED.name(), sureCount);
        Integer notSureCount = vocabularyMapper.countByCategoryIdsAndUserMark(categoryIds, UserMarkEnum.NOTSURE.getValue(), userId);
        counts.put(UserMarkEnum.NOTSURE.name(), notSureCount);
        Integer unkownCount = vocabularyMapper.countByCategoryIdsAndUserMark(categoryIds, UserMarkEnum.UNKNOWN.getValue(), userId);
        counts.put(UserMarkEnum.UNKNOWN.name(), unkownCount);
        return counts;
    }


    private void findCategoryDescendants(Long parentId, List<Category> result) {
        List<Category> children = categoryMapper.listChildrenCategory(parentId);
        result.addAll(children);
        for (Category child : children) {
            findCategoryDescendants(child.getId(), result);
        }
    }
}
